﻿using eShop.Core;
using eShop.Services;
using eShop.Web.Admin.Models;
using eShop.Web.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace eShop.Web.Admin.controllers
{
    /// <summary>
    /// 仓库信息
    /// </summary>
    public partial class StockController : BaseAdminController
    {
        public ActionResult List()
        {
            StockModel model = new StockModel();
            model.StockList = AdminStock.AdminGetStockList();
            ShopUtils.SetAdminRefererCookie(Url.Action("List"));
            return View(model);
        }


        /// <summary>
        /// 添加管理员组
        /// </summary>
        [HttpGet]
        public ActionResult Add()
        {
            StockModel model = new StockModel();
            model.StockList = AdminStock.AdminGetStockList();
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }


      


        /// <summary>
        /// 添加action
        /// </summary>
        [HttpPost]
        public ActionResult Add(StockInfo model)
        {
            //if (AdminGroups.GetAdminGroupIdByTitle(model.AdminGroupTitle) > 0)
            //    ModelState.AddModelError("AdminGroupTitle", "名称已经存在");
            if (ModelState.IsValid)
            {
                StockInfo StockInfo = new StockInfo()
                {
                    StockMc = model.StockMc,
                    StockBh = model.StockBh
                };
                AdminStock.CreateStock(StockInfo);
                AddAdminOperateLog("添加仓库", "添加仓库,仓库为:" + model.StockMc);
                return PromptView("仓库添加成功");
            }
            return View(model);
        }

        /// <summary>
        /// 编辑管理员组
        /// </summary>
        [HttpGet]
        public ActionResult Edit(int wid = -1)
        {
            StockModel model = new StockModel();
            DataTable dt = AdminStock.AdminGetStockList();
            foreach (DataRow dr in dt.Rows)
            {
                if ((int)dr["stockid"] == wid)
                {
                    model.StockBh = dr["stockbh"].ToString();
                    model.StockMc = dr["stockmc"].ToString();
                    break;
                }

            }
            if (string.IsNullOrWhiteSpace(model.StockBh))
                return PromptView("数据不存在");

            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        [HttpPost]
        public ActionResult Edit(StockModel model, int wid = -1)
        {
            StockInfo info = new StockInfo();
            DataTable dt = AdminStock.AdminGetStockList();
            foreach (DataRow dr in dt.Rows)
            {
                if ((int)dr["stockid"] == wid)
                {
                    info.StockBh = dr["stockbh"].ToString();
                    info.StockMc = dr["stockmc"].ToString();
                    break;
                }
            }
            if (string.IsNullOrWhiteSpace(info.StockBh)) return PromptView("仓库不存在");
            info.StockMc = model.StockMc;
            info.StockId = wid;
            AdminStock.UpdateStock(info);
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return PromptView("仓库修改成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        public ActionResult Del(int wid = -1)
        {
            int result = AdminStock.DeleteStockByid(wid);
            if (result == 1)
            {
                AddAdminOperateLog("删除仓库", "删除仓库,仓库Id为:" + wid);
                return PromptView("删除仓库成功");
            }
            else
            {
                return PromptView("删除失败");
            }
        }
    }
}
